package com.sikaryofficial.information.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.information.config.HISMKInformationProperties;
import com.sikaryofficial.information.constant.LanguageEnum;
import com.sikaryofficial.information.domain.dto.req.IndustryNewsListReq;
import com.sikaryofficial.information.domain.dto.resp.IndustryNewsDTO;
import com.sikaryofficial.information.manager.IndustryNewsManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author wuweihong
 * @create 2024/9/19 - 11:54
 * @description:
 **/
@RestController
@RequestMapping("/industryNews")
@Api(tags = "产业新闻服务")
public class IndustryNewsController {

    @Autowired
    private IndustryNewsManager industryNewsManager;
    @Autowired
    private HISMKInformationProperties hISMKInformationProperties;

    /**
     * 产业新闻列表
     */
    @PostMapping("/industryNewsListPage")
    @ApiOperation("产业新闻列表")
    public PageResult<IndustryNewsDTO> industryNewsListPage(@Validated @RequestBody IndustryNewsListReq req,@RequestHeader(name = "lang", required = false) String lang) {
        lang = Objects.isNull(lang) ? hISMKInformationProperties.getNewsDefaultLang() : lang;
        LanguageEnum language = LanguageEnum.getByValue(lang);
        if (Objects.isNull(language)) {
            throw new ServiceException("the language is not valid.");
        }
        req.setLang(language.getValue());
        IPage<IndustryNewsDTO> page = industryNewsManager.listPage(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 获取产业新闻详情
     */
    @GetMapping("/{industryNewsId}")
    @ApiOperation("获取产业新闻详情")
    public R<IndustryNewsDTO> getInfo(@PathVariable(value = "industryNewsId") Long industryNewsId) {
        return R.ok(industryNewsManager.getInfo(industryNewsId));
    }
}
